package info.demo.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class CryptoFilter implements Filter {
    public static final String FILTER_ENABLED = "crypto.enabled";
    private String paramName;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.paramName = filterConfig.getServletContext().getInitParameter(Crypto.INIT_PARAMETER);
        if (this.paramName == null) {
            this.paramName = Crypto.URL_PARAM_NAME;
        }
        filterConfig.getServletContext().setAttribute(CryptoFilter.FILTER_ENABLED, new Boolean(true));
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        CryptoServletRequestWrapper requestWrapper = new CryptoServletRequestWrapper((HttpServletRequest) servletRequest, paramName);
        filterChain.doFilter(requestWrapper, servletResponse);
    }

    public void destroy() {
        this.paramName = null;
    }
    
}
